package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityOrgTentPictureBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterTentPicture
import com.gexiaobao.pigeon.viewmodel.OrgDetailViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/6/14 13:55
 * @Describe    : 入棚照
 */
class ActivityOrgTentPicture : BaseActivity<OrgDetailViewModel, ActivityOrgTentPictureBinding>() {

    private val mAdapter: AdapterTentPicture by lazy { AdapterTentPicture(arrayListOf()) }
    //界面状态管理者

    private lateinit var loadsir: LoadService<Any>
    private var mRingIdOrMember = ""
    private var mRaceId = ""
    private var mOrgId = ""

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.Photo_entry)
        mDatabind.includeSearch.editQuery.hint = getString(R.string.input_search_content)
        mRaceId = intent.getIntExtra("raceId", 0).toString()
        mOrgId = intent.getIntExtra("orgId", 0).toString()

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 10f)))
            it.initFooter {
                mRingIdOrMember = mDatabind.includeSearch.editQuery.text.toString().trim()
                mViewModel.getLoftPigeonListByUserWithOrgForOpen(false, mOrgId, mRaceId, mRingIdOrMember)
            }
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            //点击重试时触发的操作
            searchResultData(true)
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            searchResultData(true)
        }

        searchResultData(true)

        mDatabind.includeSearch.editQuery.addTextChangedListener {
            mDatabind.includeSearch.ivClose.isVisible = it.toString().isNotEmpty()
        }

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    searchResultData(true)
                    hideSoftKeyboard(this)
                }

                else -> {}
            }
            false
        }
    }

    private fun onRefresh(b: Boolean) {
        loadsir.showLoading()
        mViewModel.getLoftPigeonListByUserWithOrgForOpen(b, mOrgId, mRaceId, mRingIdOrMember)
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.includeBar.ivBack,
            mDatabind.includeSearch.tvSearch,
            mDatabind.includeSearch.ivClose,
        ) {
            when (it) {
                mDatabind.includeSearch.tvSearch -> {
                    searchResultData(true)
                }

                mDatabind.includeSearch.ivClose -> {
                    mDatabind.includeSearch.editQuery.setText("")
                    searchResultData(true)
                }

                mDatabind.includeBar.ivBack -> {
                    finish()
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.tentPicData.observe(this) {
            if (it.isSuccess) {
                loadListData(it, mAdapter, loadsir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
            } else {
                RxToast.showToast(it.errMessage)
            }
        }
    }

    private fun searchResultData(isRefresh: Boolean) {
        mRingIdOrMember = mDatabind.includeSearch.editQuery.text.toString().trim()
        onRefresh(isRefresh)
    }
}